package dynamicprogramming;

/**
 * @author kaho
 * @since 2021/2/19
 */
public class __最长公共子串 {


    public int lcs(String s1, String s2){
        if (s1 == null || s2 == null) {
            return 0;
        }
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        if (c1.length==0||c2.length==0) {
            return 0;
        }

        int[][] dp = new int[c1.length][c2.length];
        int max = 0;
        for (int i = 1; i <= c1.length; i++) {
            for (int j = 1; j <= c2.length; j++) {
                if (c1[i-1]!=c2[j-1]) {
                    continue;
                }
                dp[i][j] = dp[i-1][j-1] +1;
                max = Math.max(max, dp[i][j]);
            }
        }
        return max;
    }
}
